import { Component } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { IonicPage, LoadingController, NavController, NavParams } from 'ionic-angular';
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { UtilsProvider } from "../../providers/utils/utils";
import { UrlProvider } from "../../providers/url/url";
import { ValidateProvider } from "../../providers/validate/validate";

@IonicPage({
    segment: 'task/list',
    defaultHistory: ['HomePage']
})
@Component({
    selector: 'page-task',
    templateUrl: 'task.html',
})
export class TaskPage {
    page: number=1;
    banners = [];
    notices = [];
    lists = [];
    tasks = [];
    fitter: any;
    searchParams = {};
    htmlText:any;
    constructor(
        public navCtrl: NavController,
        public sanitize:DomSanitizer,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public loadingCtrl: LoadingController,
        public util: UtilsProvider,
        public url: UrlProvider,
        public validate: ValidateProvider
    ) {
    }
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }
    ionViewDidLoad() {
        this.validate.usualShare();
    }
    ionViewWillEnter() {
        //隐藏搜索选项列表,显示搜索框
        this.searchBarStatus = true;
        this.getSearchData();
        this.getAds();
        this.getData(null);
    }
    getData(infiniteScroll) {
        //获取任务信息列表
        let params = {
            page: this.page
        };
        Object.assign(params, this.searchParams);
        this.httpService.requestPostData('/task_info/task_list', params, response => {
            if (response['code'] == 200) {
                let data = response['data'];
                //全部数据
                this.lists = this.lists.concat(data['data']);
                if (infiniteScroll) {
                    this.page++;
                    infiniteScroll.complete();
                    /*请求完成数据更新页面*/
                    if (data['data'].length < data['per_page']) {   /*没有数据 禁用上拉更新*/
                        infiniteScroll.enable(false);
                    } else {
                        // this.lists = data['data'];
                    }
                }
            } else {
                if (infiniteScroll) {
                    infiniteScroll.enable(false);
                }
            }
        });
        this.httpService.requestPostData('/mul_mission/mul_mission_list', params, response => {
            if (response['error'] == 200) {
                let data = response['data'];
                //全部数据
                this.tasks = this.tasks.concat(data['lists']);
            } else {
                this.util.presentAlert(response.message);
            }
        });
    }

    getAds() {
        //获取广告轮播
        this.httpService.requestData("/task_info/get_ads", response => {
            //轮播和滚动广告信息
            this.banners = response['data']['image_list'];
            this.notices = response['data']['text_list'];
        });
    }

    getSearchData() {
        //获取筛选项
        this.httpService.requestPostData('/task_info/search_option', {
            type: 'mission'
        }, response => {
            if (response['error'] == 200) {
                this.fitter = response['data'];
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    //跳转页面
    pageRedirect(page, data = {}) {
        this.navCtrl.push(page, data).then(success => {
            if (success !== true) {//页面需要登录后跳转
                this.navCtrl.push('LoginPage', data);
            }
        })
    }

    /**
     * 显示搜索功能
     * */
    //默认搜索框是否展示
    searchBarStatus: boolean = true;
    showSearch() {
        this.searchBarStatus = false;
    }

    hideSearch() {
        this.searchBarStatus = true;
    }

    /**
     * 搜索下拉
     * */
    searchTypeStatus = {
        time: true,
        reward_status: false,
        type: false,
        reward_sort: false
    };
    showSelect(type) {
        this.searchBarStatus = false;
        let status = this.searchTypeStatus[type];
        if (this.searchTypeStatus[type] == false) {
            this.searchTypeStatus = {
                time: false,
                reward_status: false,
                type: false,
                reward_sort: false
            };
            this.searchTypeStatus[type] = !status;
        }
    }

    /**
     * 选择筛选条件后处理
     * */
    defaultSearchData = {
        time: {
            value: 0,
            text: '时间'
        },
        type: {
            value: 0,
            text: '类别'
        },
        reward_sort: {
            value: 0,
            text: '赏金'
        },
        reward_status: {
            value: 0,
            text: '状态'
        },
        keyword: {
            value: '',
            text: ''
        }
    };
    selected(type, value, text = '') {
        text = text || this.defaultSearchData[type]['text'];
        this.defaultSearchData[type]['value'] = value;
        this.defaultSearchData[type]['text'] = text;
        this.searchBarStatus = true;

        this.searchParams[type] = value;
        this.page = 1;
        this.lists = [];
        this.getData(null);
    }

}
